package com.heartline.test;

import com.alibaba.fastjson.JSONObject;
import com.hankcs.hanlp.HanLP;
import com.heartline.pojo.Calandar;
import com.heartline.util.DateUtils;
import com.heartline.util.StringUtils;
import com.soecode.wxtools.util.crypto.MD5;
import io.github.biezhi.anima.Anima;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.junit.Before;
import org.junit.Test;
import org.sql2o.Sql2o;

import java.io.IOException;

import static io.github.biezhi.anima.Anima.select;

/**
 * Created by zhy on 2018/6/8.
 */
@Slf4j
public class ReadFile {
    @Before
    public void before() {
        Sql2o sql2o = Anima.open("jdbc:sqlite:./main.db").getSql2o();
        sql2o.setIsolationLevel(java.sql.Connection.TRANSACTION_SERIALIZABLE);
    }

    @Test
    public void coreText() throws IOException {
        try {
            Response resp = Request.Get("http://appa.applinzi.com/api/test").connectTimeout(3000).execute();
            String str = resp.returnContent().asString();

            String content = JSONObject.parseObject(str).getJSONObject("data").getString("content").trim();

//        String content = "因为我一无所有，所以肆无忌惮；因为他拥有太多，所以胆战心惊";
            log.info("接收到的数据:{}", content);
            if (StringUtils.isBlank(content)) {
                log.info("接口数据获取失败");
                return;
            }
            String md5 = MD5.getMD5(content, "UTF-8");

            String same = select().bySQL(String.class, "select date from t_calandar where md5 = ?", md5).one();
            if (StringUtils.isNotBlank(same)) {
                log.info("获取数据存在重复,date:{}", same);
                return;
            }
//        HanLP.Config.enableDebug();
            String keyWords = StringUtils.listToString(HanLP.extractKeyword(content, 5));
            String now = DateUtils.now();
            String newest = select().bySQL(String.class, "select date from t_calandar where date >= ? order by date desc", now).one();

            if (newest == null) {
                newest = now;
            } else {
                newest = DateUtils.formatDate(DateUtils.addDays(DateUtils.parseDate(newest), 1));
            }
            Calandar calandar = new Calandar(newest, keyWords, content, md5);
            calandar.save();
            log.info("日历数据保存成功{}", calandar);


        } catch (Exception e) {
            log.error("@小狐狸 你服务有问题！", e);
        }
    }

    private void getContent() {
        String weibo_url = "https://m.weibo.cn/api/container/getIndex?type=uid&value=2303934641&containerid=1076032303934641";
    }

    @Test
    public void it() {

        int[] item_id = {1, 1, 2, 3, 6, 6, 4};

        int temp = 0;
        for (int i = 0; i < item_id.length; i++) {
            if (i > 3) {
                if (i == 4) {
                    temp = item_id[i];
                } else if (item_id[i] != temp) {
                    System.out.println(i);
                    break;
                }
            }
        }
    }

}
